//
//  ALStatusOriginalView.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/14.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit

class ALStatusOriginalView: UIView {
    var status: ALStatus?{
        didSet{
            // cell将模型设置给微博原创view后,微博原创view自己来设置自己的内容
//设置头像
            if let profile_image_url = status?.user?.profile_image_url{
//            有图片地址,执行下面的代码
                let url = NSURL(string: profile_image_url)
                
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"), completed: { (image, error, _, _) -> Void in
                    if error != nil {
                        ALPrint("设置头像出错了: \(error)")
                        return
                    }
                    
                    //                    // 将图片异步绘制成圆角的
                    image.al_AsyncDrawImage(nil, isCorner: true, drawFinish: { (image) -> () in
                        self.iconView.image = image
                    })
                })
            }
            
//            用户名称
            nameLabel.text = status?.user?.screen_name
            
//            时间
            timeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()
            //            来源
            sourceLabel.text = status?.source
//            微博内容
            contentLabel.text = status?.text
            
            // 微博模型提供的数据不完整,view不能直接拿来用,还需要处理,如果多个地方要使用,这段代码需要拷贝到多个地方,当cell不停滚动的时候,是经常需要计算
            // 让模型直接提供会员等级和认证类型对应的图片
            
            memberView.image = status?.user?.mbrankImage
            
//            认证类型
            vertifiredView.image = status?.user?.vertifierImage
   
            //将微博模型的图片url设置给展示图片的view(ALPictureView),ALStautsPictureViewCellMargin自己回去计算行高
            pictureView.pictureURLs = status?.storePicutreURLs
            
//            如过没有图片视图,则将配图和textLabel之间的间距设为0
            pictureView.snp_updateConstraints { (make) in
                let margin = status?.storePicutreURLs == nil ? 0 : ALIconViewMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        
//        准备UI界面
        preparaUI()
    }
    private func preparaUI(){
//    添加UI控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(vertifiredView)
        addSubview(contentLabel)
        addSubview(pictureView)
//    设置约束
        
        iconView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(ALIconViewMargin)
            make.leading.equalTo(self).offset(ALIconViewMargin)
            make.width.height.equalTo(ALIconViewSize)
        }
        
//        名称
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(ALIconViewMargin)
        }
//        时间
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
//        来源label
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(ALIconViewMargin)
            make.centerY.equalTo(timeLabel)
        }
        
//        会员等级
        memberView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(ALIconViewMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
//        认证图标
        vertifiredView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
//        微博数据内容
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(ALIconViewMargin)
            make.top.equalTo(iconView.snp_bottom).offset(ALIconViewMargin)
            make.trailing.equalTo(self).offset(-ALIconViewMargin)
        }
        
//        设置配图的frame
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(self.snp_leading).offset(ALIconViewMargin)
//            make.trailing.equalTo(self.snp_trailing).offset(-ALIconViewMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(ALIconViewMargin)
            make.height.equalTo(CGFloat(arc4random_uniform(200)+50));
        }
        
        /// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(ALIconViewMargin)
        }
    }
    
    
//    MARK: - 懒加载控件
//    头像
    private lazy var iconView:UIImageView = {
     let icon = UIImageView(image: UIImage(named: "avatar"))
    
        icon.layer.cornerRadius = 17.5
        icon.layer.masksToBounds  = true
        return icon
    }()
//    用户名称Label
    private lazy var nameLabel:UILabel = UILabel(fontSize: ALStatusTimeFontSize, textColor:UIColor.lightGrayColor())
    
//    时间label
    private lazy var timeLabel: UILabel = UILabel(fontSize: ALStatusTimeFontSize, textColor:UIColor.orangeColor())
    //    来源label
    private lazy var sourceLabel:UILabel = UILabel(fontSize: ALStatusTimeFontSize, textColor:UIColor.lightGrayColor())
//    会员图标
    private lazy var memberView: UIImageView = UIImageView(imageNamed: "avatar_vip")
//    认证图标
    private lazy var vertifiredView : UIImageView = UIImageView(imageNamed: "avatar_grassroot")
//    原创微博类型
    private lazy var contentLabel: UILabel = {
    let label = UILabel()
    
//        设置l
        label.font = UIFont.systemFontOfSize(ALstatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        
//        设置文本内容
        let randomValue = arc4random_uniform(100)%2
        
        //设置下面的属性,如果没有设置的话,cell之间会存在间距
        //换行宽度
        // 换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * ALIconViewMargin
//        设置sizeTofit
        label.sizeToFit()
        label.numberOfLines = 0
        return label
    }()
    
//    懒加载collectionView
    
    private lazy var pictureView: ALPictureView = ALPictureView()
}
